package com.ibeeking.found.cop.b.rest.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.base.Joiner;
import com.ibeeking.found.common.config.prop.FileOssProp;
import com.ibeeking.found.common.enums.PublishStatusEnum;
import com.ibeeking.found.common.enums.SexEnum;
import com.ibeeking.found.common.utils.UrlUtils;
import com.ibeeking.found.common.utils.UserUtils;
import com.ibeeking.found.cop.api.common.dto.OssPolicyDto;
import com.ibeeking.found.cop.api.feign.FileFeignClient;
import com.ibeeking.found.cop.b.rest.service.IFriendsFollowService;
import com.ibeeking.found.cop.b.rest.service.IFriendsService;
import com.ibeeking.found.cop.b.rest.service.IMqService;
import com.ibeeking.found.cop.service.common.bo.FriendsExternalProfileBO;
import com.ibeeking.found.cop.service.common.dos.*;
import com.ibeeking.found.cop.service.common.enums.FriendsTransferChannelEnum;
import com.ibeeking.found.cop.service.common.enums.MqSyncOperatorTypeEnum;
import com.ibeeking.found.cop.service.common.enums.WxCpFriendSoruceEnum;
import com.ibeeking.found.cop.service.common.enums.WxCpFriendTypeEnum;
import com.ibeeking.found.cop.service.common.param.FriendsFollowParam;
import com.ibeeking.found.cop.service.common.param.FriendsParam;
import com.ibeeking.found.cop.service.common.param.MqSyncCopParam;
import com.ibeeking.found.cop.service.common.po.FriendsTagsRelPO;
import com.ibeeking.found.cop.service.common.query.FriendsPageQuery;
import com.ibeeking.found.cop.service.common.query.FriendsQuery;
import com.ibeeking.found.cop.service.common.vo.FriendsVO;
import com.ibeeking.found.cop.service.mapper.*;
import com.ibeeking.found.id.GenerateId;
import com.ibeeking.nematos.exception.BusinessException;
import com.ibeeking.nematos.utils.bean.BeanUtil;
import com.ibeeking.nematos.utils.data.Collections3;
import com.ibeeking.nematos.utils.date.DateUtil;
import com.ibeeking.nematos.utils.file.FileUtils;
import com.ibeeking.nematos.utils.id.IdGenerator;
import com.ibeeking.nematos.utils.json.JsonUtils;
import com.ibeeking.nematos.utils.result.ResponseResult;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @ClassName FriendsServiceImpl
 * @Description
 * @Author ibeeking
 * @Date 2021-01-27 18:50
 **/
@Service
public class FriendsServiceImpl  extends ServiceImpl<FriendsMapper, FriendsDO> implements IFriendsService {

    DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

    @Resource
    private FriendsMapper friendsMapper;

    @Resource
    private FriendsTagsRelMapper friendsTagsRelMapper;

    @Resource
    private FriendsTransferFlowMapper friendsTransferFlowMapper;

    @Resource
    private CopEmployeeMapper copEmployeeMapper;

    @Resource
    private FriendsTagsMapper friendsTagsMapper;

    @Resource
    private IFriendsFollowService friendsFollowServiceImpl;

    @Resource
    private IMqService mqServiceImpl;

    @Resource
    private FileFeignClient fileFeignClient;

    @Resource
    private FileOssProp fileOssProp;

    @Override
    public List<FriendsVO> list(FriendsQuery query) {
        String tenantId = UserUtils.getTenantId();
        List<Long> friendsIds = new ArrayList<>();
        List<FriendsVO> entityVOS = new ArrayList<>();
        Set<Long> tagIds = query.getTagsIds();
        if(tagIds!=null && tagIds.size() >0){
            List<FriendsTagsRelDO> friendsTagsRelDOS = friendsTagsRelMapper.listByTagsIds(Integer.valueOf(tenantId), tagIds);
            friendsIds = friendsTagsRelDOS.stream().map(FriendsTagsRelDO::getFriendsId).collect(Collectors.toList());
        }
        List<FriendsDO> entityDOS = friendsMapper.selectList(new LambdaQueryWrapper<FriendsDO>()
                .eq(query.getId() != null, FriendsDO::getId, query.getId())
                .eq(query.getEmployeeId() != null, FriendsDO::getEmployeeId, query.getEmployeeId())
                .eq(query.getSourceFrom() != null, FriendsDO::getSourceFrom, query.getSourceFrom())
                .eq(query.getGender() != null, FriendsDO::getGender, query.getGender())
                .eq(query.getType() != null, FriendsDO::getType, query.getType())
                .in(Collections3.isNotEmpty(friendsIds), FriendsDO::getId, friendsIds)
                .like(StringUtils.isNotBlank(query.getFriendsCode()), FriendsDO::getFriendsCode, query.getFriendsCode())
                .like(StringUtils.isNotBlank(query.getName()), FriendsDO::getName, query.getName())
                .like(StringUtils.isNotBlank(query.getMobile()), FriendsDO::getMobile, query.getMobile())
                .ge(StringUtils.isNotBlank(query.getJoinTimeBegin()), FriendsDO::getJoinTime, query.getJoinTimeBegin())
                .le(StringUtils.isNotBlank(query.getJoinTimeEnd()), FriendsDO::getJoinTime, query.getJoinTimeEnd())
                .orderByDesc(FriendsDO::getCreateTime)
        );
        if(Collections3.isNotEmpty(entityDOS)) {
            List<CopEmployeeDO> copEmployeeDOS = copEmployeeMapper.selectList(new LambdaQueryWrapper<>());
            Map<Long, String> copEmployeeNameMaps = copEmployeeDOS.stream().collect(Collectors.toMap(CopEmployeeDO::getId, CopEmployeeDO::getEmployeeName, (o, n) -> n));

            entityVOS = entityDOS.stream().map(f -> {
                FriendsVO x = BeanUtil.convertBean(f, FriendsVO.class);
                Long friendsId = x.getId();
                List<FriendsTagsRelPO> friendsTagsRelPOS = friendsTagsRelMapper.listByFriendsId(Integer.valueOf(tenantId), friendsId);
                if (friendsTagsRelPOS != null && friendsTagsRelPOS.size() > 0) {
                    List<String> friendsTagsList = friendsTagsRelPOS.stream().map(FriendsTagsRelPO::getTagsName).collect(Collectors.toList());
                    String friendsTagsStr = StringUtils.join(friendsTagsList, ",");
                    x.setFriendsTagsStr(friendsTagsStr);
                    x.setFriendsTagsRel(friendsTagsRelPOS);
                }
                String avatar = x.getAvatar();
                if (StringUtils.isNotEmpty(avatar)) {
                    avatar = UrlUtils.addUrlDomainName(fileOssProp.getHost(), avatar);
                    x.setAvatar(avatar);
                }
                x.setEmployeeName(copEmployeeNameMaps!=null?copEmployeeNameMaps.get(x.getEmployeeId()):"");
                x.setTypeName(WxCpFriendTypeEnum.getDescByCode(x.getType()));
                x.setGenderDesc(SexEnum.getDescByCode(x.getGender()));
                x.setSourceFromDesc(WxCpFriendSoruceEnum.getDescByCode(x.getSourceFrom()));
                x.setStatusDesc(PublishStatusEnum.getName(x.getStatus()));
                return x;
            }).collect(Collectors.toList());
        }
        return entityVOS;
    }

    @Override
    public Page<FriendsVO> pageList(FriendsPageQuery pageQuery) {
        String tenantId = UserUtils.getTenantId();
        List<Long> friendsIds = new ArrayList<>();
        Set<Long> tagIds = pageQuery.getTagsIds();
        if(tagIds!=null && tagIds.size() >0){
            List<FriendsTagsRelDO> friendsTagsRelDOS = friendsTagsRelMapper.listByTagsIds(Integer.valueOf(tenantId), tagIds);
            friendsIds = friendsTagsRelDOS.stream().map(FriendsTagsRelDO::getFriendsId).collect(Collectors.toList());
        }
        LambdaQueryWrapper<FriendsDO> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(pageQuery.getId() != null, FriendsDO::getId, pageQuery.getId())
                .in(Collections3.isNotEmpty(friendsIds), FriendsDO::getId, friendsIds)
                .eq(pageQuery.getEmployeeId() != null, FriendsDO::getEmployeeId, pageQuery.getEmployeeId())
                .eq(pageQuery.getSourceFrom() != null, FriendsDO::getSourceFrom, pageQuery.getSourceFrom())
                .eq(pageQuery.getGender() != null, FriendsDO::getGender, pageQuery.getGender())
                .eq(pageQuery.getType() != null, FriendsDO::getType, pageQuery.getType())
                .like(StringUtils.isNotBlank(pageQuery.getFriendsCode()), FriendsDO::getFriendsCode, pageQuery.getFriendsCode())
                .like(StringUtils.isNotBlank(pageQuery.getName()), FriendsDO::getName, pageQuery.getName())
                .like(StringUtils.isNotBlank(pageQuery.getMobile()), FriendsDO::getMobile, pageQuery.getMobile())
                .ge(StringUtils.isNotBlank(pageQuery.getJoinTimeBegin()), FriendsDO::getJoinTime, pageQuery.getJoinTimeBegin())
                .le(StringUtils.isNotBlank(pageQuery.getJoinTimeEnd()), FriendsDO::getJoinTime, pageQuery.getJoinTimeEnd())
                .orderByDesc(FriendsDO::getCreateTime);
        Page<FriendsDO> entityDOPages = friendsMapper.selectPage(new Page<FriendsDO>(pageQuery.getPageNum(), pageQuery.getPageSize()), wrapper);
        Page<FriendsVO> entityVOPages = BeanUtil.convertPage(entityDOPages, FriendsVO.class);
        if(entityVOPages!= null && entityVOPages.getSize() >0 ) {
            List<CopEmployeeDO> copEmployeeDOS = copEmployeeMapper.selectList(new LambdaQueryWrapper<CopEmployeeDO>());
            Map<Long, String> copEmployeeNameMaps =copEmployeeDOS.stream().collect(Collectors.toMap(CopEmployeeDO::getId, CopEmployeeDO::getEmployeeName, (o, n) -> n));
            entityVOPages.getRecords().forEach(x -> {
                Long friendsId = x.getId();
                List<FriendsTagsRelPO> friendsTagsRelPOS =  friendsTagsRelMapper.listByFriendsId(Integer.valueOf(tenantId), friendsId);
                if(friendsTagsRelPOS!=null && friendsTagsRelPOS.size()>0) {
                    List<String> friendsTagsList = friendsTagsRelPOS.stream().map(FriendsTagsRelPO::getTagsName).collect(Collectors.toList());
                    String friendsTagsStr = StringUtils.join(friendsTagsList, ",");
                    x.setFriendsTagsStr(friendsTagsStr);
                    x.setFriendsTagsRel(friendsTagsRelPOS);
                }
                String avatar = x.getAvatar();
                if(StringUtils.isNotEmpty(avatar)){
                    avatar = UrlUtils.addUrlDomainName(fileOssProp.getHost(), avatar);
                    x.setAvatar(avatar);
                }
                x.setEmployeeName(copEmployeeNameMaps!=null?copEmployeeNameMaps.get(x.getEmployeeId()):"");
                x.setTypeName(WxCpFriendTypeEnum.getDescByCode(x.getType()));
                x.setGenderDesc(SexEnum.getDescByCode(x.getGender()));
                x.setSourceFromDesc(WxCpFriendSoruceEnum.getDescByCode(x.getSourceFrom()));
                x.setStatusDesc(PublishStatusEnum.getName(x.getStatus()));
            });
        }
        return entityVOPages;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public Boolean add(FriendsParam param) {
        checkUnique(param);
        FriendsDO entityDO = BeanUtil.convertBean(param, FriendsDO.class);
        entityDO.setId(GenerateId.generateId());
        entityDO.setCreateBy(UserUtils.getUserId());
        entityDO.setCreateTime(LocalDateTime.now());
        entityDO.setModifyTime(LocalDateTime.now());
        return friendsMapper.insert(entityDO) > 0;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public Boolean modify(FriendsParam param) {
        FriendsDO entityDO = BeanUtil.convertBean(param, FriendsDO.class);
        entityDO.setModifyBy(UserUtils.getUserId());
        entityDO.setModifyTime(LocalDateTime.now());
        return friendsMapper.updateById(entityDO) > 0;
    }

    @Override
    public FriendsVO view(Long id) {
        String tenantId = UserUtils.getTenantId();
        FriendsDO entityDO = friendsMapper.selectById(id);
        if(entityDO == null) throw new BusinessException("没有查到相关记录！");
        FriendsVO entityVO = BeanUtil.convertBean(entityDO, FriendsVO.class);
        if(entityVO != null) {
            List<CopEmployeeDO> copEmployeeDOS = copEmployeeMapper.selectList(new LambdaQueryWrapper<>());
            Map<Long, String> copEmployeeNameMaps =copEmployeeDOS.stream().collect(Collectors.toMap(CopEmployeeDO::getId, CopEmployeeDO::getEmployeeName, (o, n) -> n));
            entityVO.setEmployeeName(copEmployeeNameMaps.get(entityVO.getEmployeeId()));
            List<FriendsTagsRelPO> friendsTagsRelPOS =  friendsTagsRelMapper.listByFriendsId(Integer.valueOf(tenantId), id);
            if(friendsTagsRelPOS!=null && friendsTagsRelPOS.size()>0) {
                List<String> friendsTagsList = friendsTagsRelPOS.stream().map(FriendsTagsRelPO::getTagsName).collect(Collectors.toList());
                String friendsTagsStr = StringUtils.join(friendsTagsList, ",");
                entityVO.setFriendsTagsStr(friendsTagsStr);
                entityVO.setFriendsTagsRel(friendsTagsRelPOS);
            }
            entityVO.setTypeName(WxCpFriendTypeEnum.getDescByCode(entityVO.getType()));
            entityVO.setGenderDesc(SexEnum.getDescByCode(entityVO.getGender()));
            entityVO.setSourceFromDesc(WxCpFriendSoruceEnum.getDescByCode(entityVO.getSourceFrom()));
            entityVO.setStatusDesc(PublishStatusEnum.getName(entityVO.getStatus()));
            entityVO.setJoinTime(dateTimeFormatter.format(entityDO.getJoinTime()));
        }
        return entityVO;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public Boolean changeStatus(Long id, Integer status) {
        FriendsDO entityDO = friendsMapper.selectById(id);
        if(entityDO == null) throw new BusinessException("没有查到相关记录！");
        entityDO.setModifyBy(UserUtils.getUserId());
        entityDO.setModifyTime(LocalDateTime.now());
        return friendsMapper.updateById(entityDO) > 0;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public Boolean del(Long id) {
        FriendsDO entityDO = friendsMapper.selectById(id);
        if(entityDO == null) throw new BusinessException("没有查到相关记录！");
        return friendsMapper.deleteById(id) > 0;
    }

    /**
     * 从企业微信同步客户信息
     *
     * @param
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public Integer syncFriendsFromWx(){
        String tenantId = UserUtils.getTenantId();
        MqSyncCopParam mqParam = new MqSyncCopParam();
        mqParam.setTenantId(Long.valueOf(tenantId));
        mqParam.setOperType(MqSyncOperatorTypeEnum.COP_SYNC_FRIENDS_LIST.getCode());
        mqServiceImpl.sendSyncToWxTaskMessage(mqParam);
        return 1;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public Integer saveOrUpdateOrDeleteBatchFriends(Long tenantId, List<FriendsParam> params){
        if(Collections3.isNotEmpty(params)) {
            List<Long> friendsIds = new ArrayList<>();
            List<FriendsDO> needInsertFriendsDOS = new ArrayList<>();
            List<FriendsDO> needUpdateFriendsDOS = new ArrayList<>();
            List<FriendsDO> needDeleteFriendsDOS = new ArrayList<>();
            List<FriendsTagsRelDO> needInsertFriendsTagsRelDOS = new ArrayList<>();
            List<FriendsTransferFlowDO> needInsertFriendsTransferFlowDOS = new ArrayList<>();
            Map<String, Long> frendsCodeAndIdMap = null;
            List<FriendsFollowParam> allFriendsFollowParamList = new ArrayList<>();

            List<FriendsDO> oldFriendsDOS = friendsMapper.selectList(new LambdaQueryWrapper<FriendsDO>());
            if(Collections3.isNotEmpty(oldFriendsDOS)) {
                frendsCodeAndIdMap = oldFriendsDOS.parallelStream().collect(Collectors.toMap(FriendsDO::getFriendsCode, FriendsDO::getId, (o, n) -> o));
                List<String> oldFriendsCodeIdList = oldFriendsDOS.parallelStream().map(FriendsDO::getFriendsCode).collect(Collectors.toList());
                List<String> newFriendsCodeList = params.parallelStream().map(FriendsParam::getFriendsCode).collect(Collectors.toList());
                if (Collections3.isNotEmpty(oldFriendsCodeIdList) && Collections3.isNotEmpty(newFriendsCodeList)) {
                    HashSet<String> oldFriendsCodeHash = new HashSet<>(oldFriendsCodeIdList);
                    HashSet<String> newFriendsCodeHash = new HashSet<>(newFriendsCodeList);
                    oldFriendsCodeHash.removeAll(newFriendsCodeHash);
                    if (oldFriendsCodeHash.size() > 0) {
                        List<String> needDeleteFriendsCodes = new ArrayList<>(oldFriendsCodeHash);
                        needDeleteFriendsDOS = friendsMapper.selectList(new LambdaQueryWrapper<FriendsDO>().in(FriendsDO::getFriendsCode, needDeleteFriendsCodes));
                    }
                }
            }
            List<CopEmployeeDO> copEmployeeDOList = copEmployeeMapper.selectList(new LambdaQueryWrapper<CopEmployeeDO>());
            Map<String, Long> copEmployeeNameMaps = Collections3.isNotEmpty(copEmployeeDOList)?copEmployeeDOList.stream().collect(Collectors.toMap(CopEmployeeDO::getEmployeeCode, CopEmployeeDO::getId, (o, n) -> n)):null;
            List<FriendsTagsDO> friendsTagsDOList = friendsTagsMapper.selectList(new LambdaQueryWrapper<FriendsTagsDO>());
            Map<String, Long> friendsTagsCodeMaps = Collections3.isNotEmpty(friendsTagsDOList)?friendsTagsDOList.stream().collect(Collectors.toMap(FriendsTagsDO::getTagsCode, FriendsTagsDO::getId, (o, n) -> n)):null;
            OssPolicyDto ossPolicyDto = getOssFilePolicy();
            for(FriendsParam d:params){
                FriendsDO friendsDO = BeanUtil.convertBean(d, FriendsDO.class);
                Long friendsId = 0L;
                Long employeeId = null;
                FriendsExternalProfileBO friendsExternalProfileBO = d.getExternalProfileBO();
                if(friendsExternalProfileBO !=null){
                    friendsDO.setExternalProfile(JsonUtils.toJsonStr(friendsExternalProfileBO));
                }else{
                    friendsDO.setExternalProfile(null);
                }
                if(StringUtils.isNotEmpty(d.getEmployeeCode())){
                    employeeId = copEmployeeNameMaps.get(d.getEmployeeCode());
                    friendsDO.setEmployeeId(employeeId);
                }
                String avatarUrl = d.getAvatarUrl();
                if(StringUtils.isNotEmpty(avatarUrl)){
                    String avatar = transferFileToOss(d.getFriendsCode() + "_avatar", avatarUrl, ossPolicyDto);
                    friendsDO.setAvatar(avatar);
                }
                if(frendsCodeAndIdMap!=null && frendsCodeAndIdMap.containsKey(d.getFriendsCode())){
                    friendsId = frendsCodeAndIdMap.get(d.getFriendsCode());
                    friendsDO.setId(friendsId);
                    friendsDO.setModifyTime(LocalDateTime.now());
                    needUpdateFriendsDOS.add(friendsDO);
                    friendsIds.add(friendsId);
                }else{
                    friendsDO.setCreateTime(LocalDateTime.now());
                    friendsDO.setModifyTime(LocalDateTime.now());
                    if(StringUtils.isNotEmpty(d.getJoinTime())){
                        friendsDO.setJoinTime(DateUtil.strToLocalDateTime(d.getJoinTime()));
                    }
                    friendsId = IdGenerator.getId();
                    friendsDO.setId(friendsId);
                    needInsertFriendsDOS.add(friendsDO);
                    friendsIds.add(friendsId);
                    //记录客户跟进记录
                    FriendsTransferFlowDO friendsTransferFlowDO = new FriendsTransferFlowDO();
                    friendsTransferFlowDO.setId(IdGenerator.getId());
                    friendsTransferFlowDO.setFriendsId(friendsId);
                    friendsTransferFlowDO.setFriendsCode(d.getFriendsCode());
                    friendsTransferFlowDO.setEmployeeId(employeeId);
                    friendsTransferFlowDO.setEmployeeCode(d.getEmployeeCode());
                    friendsTransferFlowDO.setTransferChannel(FriendsTransferChannelEnum.TRANSFER_CHANNEL_INVITE.getCode());
                    friendsTransferFlowDO.setTransferTime(LocalDateTime.now());
                    needInsertFriendsTransferFlowDOS.add(friendsTransferFlowDO);
                }
                List<String> allRemarkMobiles = new ArrayList<>();
                List<String> allTagsCodes = new ArrayList<>();
                List<FriendsFollowParam> friendsFollowParamList = d.getFriendsFollowList();
                if(Collections3.isNotEmpty(friendsFollowParamList)){
                    for(FriendsFollowParam followParam: friendsFollowParamList){
                        followParam.setFriendsId(friendsId);
                        followParam.setEmployeeId(employeeId);
                        String remarkMobileJson = followParam.getRemarkMobilesJson();
                        if(StringUtils.isNotEmpty(remarkMobileJson)){
                            List<String> remarkMobileList = JsonUtils.jsonToList(remarkMobileJson, String.class);
                            allRemarkMobiles.addAll(remarkMobileList);
                        }
                        String remarkTagIdsJson = followParam.getRemarkTagIdsJson();
                        if(StringUtils.isNotEmpty(remarkTagIdsJson)){
                            List<String> tagsIdArray = JsonUtils.jsonToList(remarkTagIdsJson, String.class);
                            allTagsCodes.addAll(tagsIdArray);
                        }
                    }
                    allFriendsFollowParamList.addAll(friendsFollowParamList);
                }
                if(Collections3.isNotEmpty(allRemarkMobiles)){
                    friendsDO.setMobile(Joiner.on(";").join(allRemarkMobiles));
                }
                if(Collections3.isNotEmpty(allTagsCodes)){
                    for(String tagsCode: allTagsCodes){
                        if(friendsTagsCodeMaps.containsKey(tagsCode)) {
                            Long tagsRel = IdGenerator.getId();
                            Long tagsId = friendsTagsCodeMaps.get(tagsCode);
                            FriendsTagsRelDO friendsTagsRelDO = new FriendsTagsRelDO(tagsRel, friendsId, tagsId);
                            needInsertFriendsTagsRelDOS.add(friendsTagsRelDO);
                        }
                    }
                }
            }
            if(Collections3.isNotEmpty(needDeleteFriendsDOS)){
                List<Long> delIds = needDeleteFriendsDOS.stream().map(FriendsDO::getId).collect(Collectors.toList());
                friendsMapper.deleteBatchIds(delIds);
            }

            if(Collections3.isNotEmpty(needInsertFriendsDOS)){
                friendsMapper.insertBatchSomeColumn(needInsertFriendsDOS);
            }
            if(Collections3.isNotEmpty(needUpdateFriendsDOS)){
                super.updateBatchById(needUpdateFriendsDOS);
            }
            if(Collections3.isNotEmpty(allFriendsFollowParamList)){
                friendsFollowServiceImpl.saveOrUpdateOrDeleteBatchFriendsFollow(tenantId, allFriendsFollowParamList);
            }
            if(Collections3.isNotEmpty(needInsertFriendsTagsRelDOS) && Collections3.isNotEmpty(friendsIds)){
                friendsTagsRelMapper.delete(new LambdaQueryWrapper<FriendsTagsRelDO>().in(FriendsTagsRelDO::getFriendsId,friendsIds));
                friendsTagsRelMapper.insertBatchSomeColumn(needInsertFriendsTagsRelDOS);
            }
            if(Collections3.isNotEmpty(needInsertFriendsTransferFlowDOS)){
                friendsTransferFlowMapper.insertBatchSomeColumn(needInsertFriendsTransferFlowDOS);
            }
        }
        return 1;
    }


    private String transferFileToOss(String id, String fileUrl, OssPolicyDto policy){
        try {
            String fileName = (String.valueOf(System.currentTimeMillis())).concat("_".concat(id).concat(".png"));
            String fileTag = policy.getDir() + fileName;
            MultipartFile multipartFile = FileUtils.getMultipartFile(fileUrl, fileName, fileTag);
            ResponseResult<String> responseResult = fileFeignClient.uploadFile(multipartFile);
            String fileUploadPath = responseResult.getData();
            return UrlUtils.removeUrlDomainName(fileUploadPath);
        } catch (Exception ex) {
            throw new BusinessException("上传头像文件失败：" + ex.toString());
        }
    }

    private OssPolicyDto getOssFilePolicy(){
        ResponseResult<OssPolicyDto> responseResult = fileFeignClient.policy();
        return responseResult.getData();
    }

    /**
     * 校验唯一性
     *
     * @param param
     */
    public void checkUnique(FriendsParam param) {
        if (param == null) {
            throw new BusinessException("当前记录不存在!");
        }
        //code name确保等级唯一性
        LambdaQueryWrapper<FriendsDO> queryWrapper = new LambdaQueryWrapper<FriendsDO>()
                .ne(param.getId() != null, FriendsDO::getId, param.getId())
                .eq(FriendsDO::getName, param.getName());
        List<FriendsDO> dos = friendsMapper.selectList(queryWrapper);
        if (Collections3.isNotEmpty(dos)) {
            throw new BusinessException("已经存在同名的记录!");
        }
    }

}
